package com.example.mymvvmdemo.utils;

import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.mymvvmdemo.MyApplication;
import com.example.mymvvmdemo.R;


public class ToastUtils implements View.OnClickListener {

    public static int TOA_S = 1;// 成功
    public static int TOA_F = 2;// 失败
    public static int TOA_W = 3;// 警告

    private static ToastUtils toastUtils;
    private static Toast mToast;
    private TextView text;
    private View tosView;
    private ImageView ivImg;
    private ConstraintLayout clAll;
    private ConstraintLayout clDefault;

    public ToastUtils() {
        try {
            //View布局
            tosView = LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.toast_layout, null);
            text = tosView.findViewById(R.id.tv_context);
            ivImg = tosView.findViewById(R.id.iv_img);
            clAll = tosView.findViewById(R.id.cl_all);
            clDefault = tosView.findViewById(R.id.cl_default);
        } catch (Exception e) {
            LogUtils.e("toast·init error" + e.getMessage());
        }
    }

    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
            mToast = null; //toast隐藏后，将其置为null
            mHandler.removeCallbacks(r);
        }
    };


    public static ToastUtils getToastUtils() {
        synchronized (String.class) {
            if (toastUtils == null) {
                synchronized (String.class) {
                    toastUtils = new ToastUtils();
                }
            }
        }
        return toastUtils;
    }

    public void toast(String message, int type) {
        try {
            initAll();
            text.setText(message);
            setShowImage(type);
            initToast();
            mHandler.postDelayed(r, 3000); //延迟3秒去掉toast,防止一直出不来
            mToast.show();
        } catch (NullPointerException e) {
            LogUtils.e("toast error:" + e.getMessage());
        }
    }

    public void toast(Context context, int resId, int type) {
        try {
            initAll();
            String message = context.getString(resId);
            text.setText(message);
            setShowImage(type);
            initToast();
            mHandler.postDelayed(r, 3000); //延迟3秒去掉toast,防止一直出不来
            mToast.show();
        } catch (Exception e) {
            LogUtils.e("toast error:" + e.getMessage());
        }
    }

    private void setShowImage(int type) {
        if (type == TOA_S) {
            ivImg.setImageResource(R.drawable.ic_svg_toast_success_white);
        } else if (type == TOA_F) {
            ivImg.setImageResource(R.drawable.ic_svg_toast_fail_white);
        } else if (type == TOA_W) {
            ivImg.setImageResource(R.drawable.ic_svg_toast_wrong_white);
        }
    }

    private void initToast() {
        if (mToast == null) {
            mToast = new Toast(MyApplication.getInstance());   //上下文
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setDuration(Toast.LENGTH_SHORT);  //短暂提示
            mToast.setView(tosView);   //把View布局设置到Toast里面
        }
    }

    /**
     * 得到屏幕高度
     */
    public int getScreenHeight() {
        WindowManager windowManager = (WindowManager) MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }

    /**
     * 得到屏幕宽度
     */
    public int getScreenWidth() {
        WindowManager windowManager = (WindowManager) MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    private void initAll() {
        ViewGroup.LayoutParams layoutParams = clAll.getLayoutParams();
        layoutParams.width = getScreenWidth();
        layoutParams.height = getScreenHeight();
        clAll.setLayoutParams(layoutParams);
    }

    /************************点击事件***************************/
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.cl_all:
//                LogUtils.d("嗨嗨嗨");
//                break;
        }
    }
}
